//
//  TTBeaconRegion.m
//  iBeaconDemo
//
//  Created by CRY on 17/1/5.
//  Copyright © 2017年 eGova. All rights reserved.
//

#import "TTBeaconRegion.h"

@implementation TTBeaconRegion

- (instancetype)initWithName:(NSString *)name
                        uuid:(NSUUID *)uuid
                       major:(CLBeaconMajorValue)major
                       minor:(CLBeaconMinorValue)minor{
    
    self = [super init];
    if (!self) {
        return nil;
    }
    _name = name;
    _uuid = uuid;
    _majorValue = major;
    _minorValue = minor;
    
    return self;
}

- (instancetype)initWithName:(NSString *)name
                        uuid:(NSUUID *)uuid
                       major:(CLBeaconMajorValue)major
                       minor:(CLBeaconMinorValue)minor
                        rssi:(NSInteger)rssi{
    
    if (self = [self initWithName:name uuid:uuid major:major minor:minor]) {
        _rssi = rssi;
    }
    return self;
}

- (BOOL)isEqualToCLBeacon:(CLBeacon *)beacon {
//    &&
//    [beacon.major isEqual: @(self.majorValue)] &&
//    [beacon.minor isEqual: @(self.minorValue)]
    if ([[beacon.proximityUUID UUIDString] isEqualToString:[self.uuid UUIDString]]){
        return YES;
    } else {
        return NO;
    }
}

- (CLBeaconRegion *)convertToBeaconRegion{
    
//    CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:self.uuid
//                                                                           major:self.majorValue
//                                                                           minor:self.minorValue identifier:self.name];
    CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:self.uuid identifier:@"identifier"];
    return beaconRegion;
}

- (NSComparisonResult)compareByRSSI:(TTBeaconRegion *)beacon{
    
    if (self.rssi < beacon.rssi) {
        return NSOrderedAscending;
    }else{
        return NSOrderedDescending;
    }
}

- (NSString *)keyID{
    return [NSString stringWithFormat:@"%@_%hd_%hu",self.uuid.UUIDString,self.majorValue,self.minorValue];
}

@end
